package com.uvlab.sitespot.io;


import com.uvlab.sitespot.common.JxNode;
import com.uvlab.sitespot.service.JxAppService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;

/**
 * Created by Administrator on 2016/4/20.
 * modified by yl on 2016/5/9   --------- RESTClient error
 * modefied by jys 2016-6-28 11:15:232  using restful url
 */
@CrossOrigin
@RestController
@RequestMapping(value = "/rest/site")
public class JxSiteEditorController {
    @Resource(name="svc")
    JxAppService svc;
    //查(ok)
    @RequestMapping(method = RequestMethod.GET)
    public HashMap<String,JxNode> find() {
        return svc.newAppSiteEditorService().find();
    }
    //切换节点信息
    @RequestMapping(method = RequestMethod.GET,value = "/{id:.+}/children")
    public HashMap<String,JxNode> change(@PathVariable(value = "id") String id) {
        String tmpid=svc.newAppSiteEditorService().getFromBase64(id);
        return svc.newAppSiteEditorService().find(tmpid);
    }
    //得到单个指定id节点信息(ok)
    @RequestMapping(method = RequestMethod.GET,value = "/{id:.+}")
    public JxNode get(@PathVariable(value = "id") String id) {
        String tmpid=svc.newAppSiteEditorService().getFromBase64(id);
         return svc.newAppSiteEditorService().get(tmpid);
    }
    //删
    @RequestMapping(method = RequestMethod.DELETE,value = "/{id:.+}")
    public void remove(@PathVariable(value = "id") String id){
        String tmpid=svc.newAppSiteEditorService().getFromBase64(id);
         svc.newAppSiteEditorService().remove(tmpid);
    }


    //改(ok)
   // ,produces={"application/json"},consumes={"application/json"}
/*    @RequestMapping(method = RequestMethod.POST,value = "/content")
    public JxNode set(@RequestParam("id") String id,@RequestParam("content") String content){
        return svc.newAppSiteEditorService().set(id, content);
    }*/
    @RequestMapping(method = RequestMethod.PUT,value = "/{id:.+}")
    public JxNode set(@PathVariable(value = "id") String id,@RequestBody JxNode node){
        String tmpid=svc.newAppSiteEditorService().getFromBase64(id);
        return svc.newAppSiteEditorService().set(tmpid,node);
        //return null;
    }
    //增(ok)
    @RequestMapping(method = RequestMethod.POST)
     public JxNode add(@RequestBody JxNode node){
        return svc.newAppSiteEditorService().add(node);
    }
}
